package com.example.webbasic.filter;

import java.io.IOException;
import java.util.function.Consumer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.springframework.core.annotation.Order;
import org.springframework.http.HttpRequest;

/**
 * MyFilter3
 * 检查 过滤器 顺序 
 * @author ben
 * @date 2021-11-06 17:04:08 CST
 */
@WebFilter(filterName = "myFilter3", urlPatterns = {"/a/*", "/b/*", "/s/*"})
// 更改优先级，实现在 MyFilter2 之前执行
// 无用
//@Order(-100)
public class MyFilter3 implements Filter {

	private static Consumer<Object> cs = System.out::println;
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		cs.accept("MyFilter3 过滤器初始化：" + MyFilter3.class);
	}
	
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		// 可以做什么呢？
		cs.accept("MyFilter3 req=" + req + ", param.key=" + req.getParameter("key"));
		cs.accept("MyFilter3 attr.key=" + req.getAttribute("key"));
		cs.accept("MyFilter3 执行过滤操作...scheme=" + req.getScheme());
		chain.doFilter(req, resp);
	}
	
	@Override
	public void destroy() {
		cs.accept("MyFilter3 过滤器销毁：" + MyFilter3.class);
	}

}
